﻿using LibNiconico.NiconicoApi.Alert;
using LibNiconico.NiconicoApi.Exceptions;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Internal.Http;
using LibNiconico.NiconicoApi.Lives.Constant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibNiconico.NiconicoApi
{
    /// <summary>
    /// ニコニコアラートAPI
    /// </summary>
    public class NicoAlertApiHelper
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public NicoAlertApiHelper()
        {
            this.auth = new NiconicoAlertAuth("");
        }

        private NiconicoAlertAuth auth;

        /// <summary>
        /// 認証情報が設定されているか判定
        /// </summary>
        public bool IsSetAuth
        {
            get
            {
                return this.auth.IsSetAuth;
            }
        }


        

        /// <summary>
        /// 非同期にログイン
        /// </summary>
        /// <param name="mail"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<NicoAlertApiLoginResponse> LoginAsync(string mail, string password)
        {
            return await this.LoginAsync(new NicoAlertApiLoginQueryInfo(mail, password)).ConfigureAwait(false);
        }

        internal async Task<NicoAlertApiLoginResponse> LoginAsync(NicoAlertApiLoginQueryInfo queryInfo)
        {
            using(var api = new NiconicoInfoGetApiXmlImpl(NicoSecureConstant.LOGIN_URL, NiconicoHttpClient.CreateNiconicoAlertApi()))
            {
               var response = NicoAlertApiLoginResponse.Parse((await api.PostAsync(queryInfo).ConfigureAwait(false)).Content);

               if (response.Succeed)
               {
                   this.auth = response.Auth;
               }

               return response;
            }
        }


        /// <summary>
        /// getalertStatus
        /// 要ログイン
        /// </summary>
        /// <returns></returns>
        public async Task<NicoAlertStatusResponse> GetAlertStatusAsync()
        {
            if (!this.auth.IsSetAuth)
            {
                throw NicoApiException.CreateLoggedException();
            }

            return await this.GetAlertStatusAsync(new NicoAlertStatusQueryInfo(this.auth.Ticket)).ConfigureAwait(false);

        }

        internal async Task<NicoAlertStatusResponse> GetAlertStatusAsync(NicoAlertStatusQueryInfo queryInfo)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoLiveConstant.NICOLIVE_URL, NiconicoHttpClient.CreateNiconicoAlertApi()))
            {

                var response = NicoAlertStatusResponse.Parse((await api.PostAsync(queryInfo).ConfigureAwait(false)).Content);
                return response;
            }
        }

        /// <summary>
        /// getalertinfoAPI情報取得
        /// ログイン不要
        /// </summary>
        /// <returns></returns>
        public async Task<NicoAlertInfoResponse> GetAlertInfoAsync()
        {
            return await this.GetAlertInfoAsync(new NicoAlertInfoQueryInfo()).ConfigureAwait(false);
        }

        /// <summary>
        /// ニコニコアラートストリーム情報取得
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<NicoAlertStreamInfoResponse> GetStreamInfoAsync(string id)
        {

            return await this.GetStreamInfoAsync(new NicoAlertStreamInfoQueryInfo(id)).ConfigureAwait(false);
        }


        internal async Task<NicoAlertStreamInfoResponse> GetStreamInfoAsync(NicoAlertStreamInfoQueryInfo queryInfo)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoLiveConstant.NICOLIVE_URL, NiconicoHttpClient.CreateNiconicoAlertApi()))
            {
                var response = NicoAlertStreamInfoResponse.Parse((await api.GetAsync(queryInfo).ConfigureAwait(false)).Content);
                return response;
            }
        }

        internal async Task<NicoAlertInfoResponse> GetAlertInfoAsync(NicoAlertInfoQueryInfo queryInfo)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoLiveConstant.NICOLIVE_URL, NiconicoHttpClient.CreateNiconicoAlertApi()))
            {
                var response = NicoAlertInfoResponse.Parse((await api.GetAsync(queryInfo).ConfigureAwait(false)).Content);
                return response;
            }
        }

    }
}
